package com.ebelter.bpm.adapters;

import android.content.Context;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ebelter.bpm.R;
import com.ebelter.btcomlib.models.https.responses.GetBloodPressureData1;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class BpmHostoryListAdapter extends BaseAdapter {


    private static final String TAG = "HostoryListAdapter";
    private Context mContext;

    private int redColor= Color.parseColor("#ff0000");
    private int whiteColor= Color.parseColor("#ffffff");

    public BpmHostoryListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public List<GetBloodPressureData1.ResultDataBean.DataBean> getData() {
        return data;
    }

    private List<GetBloodPressureData1.ResultDataBean.DataBean> data;

    public void setData(List<GetBloodPressureData1.ResultDataBean.DataBean> data) {

        this.data=data;
        notifyDataSetChanged();
    }

    public void addData(List<GetBloodPressureData1.ResultDataBean.DataBean> data2) {
        if (data==null){
           data=new ArrayList<>();
        }
       data.addAll(data2);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Vh vh;
        if (convertView==null){
            convertView=View.inflate(mContext, R.layout.bpmhostory_item,null);
            vh=new Vh();
            vh.date_tv=convertView.findViewById(R.id.bpm_item_date_tv);
            vh.time_tv=convertView.findViewById(R.id.bpm_item_time_tv);
            vh.ssy_tv=convertView.findViewById(R.id.bpm_his_ssy_tv);
            vh.szhy_tv=convertView.findViewById(R.id.bpm_his_szhy_tv);
            vh.maibo_tv=convertView.findViewById(R.id.bpm_his_maibo_tv);

            convertView.setTag(vh);
        }else {
            vh= (Vh) convertView.getTag();
        }
        GetBloodPressureData1.ResultDataBean.DataBean bean=data.get(position);
        Date date=TimeUtils.parseFormatter1Time(bean.getTestDate());
        vh.date_tv.setText(TimeUtils.formatDate_nyr(mContext,date.getTime()));
        vh.time_tv.setText(TimeUtils.formatTime_shf(mContext,date.getTime()));

        LogUtils.i(TAG,"bean.getTestDate()="+bean.getTestDate()+"----"+TimeUtils.formatDate_nyr(mContext,date.getTime())+"------"+TimeUtils.formatTime_shf(mContext,date.getTime()));


        setSSY(vh.ssy_tv,bean.getSystolic());
        setSZHY(vh.szhy_tv,bean.getDiastolic());
        vh.maibo_tv.setText(bean.getPulse()+"");
        return convertView;
    }




    private void setSSY(TextView textView,int value){
        if (value<90||value>=140){
            textView.setTextColor(redColor);
        }else {
            textView.setTextColor(whiteColor);
        }
        textView.setText(value+"");
    }
    private void setSZHY(TextView textView,int value){
        if (value<60||value>=90){
            textView.setTextColor(redColor);
        }else {
            textView.setTextColor(whiteColor);
        }
        textView.setText(value+"");
    }





    private class Vh{
        TextView date_tv;
        TextView time_tv;
        TextView ssy_tv;
        TextView szhy_tv;
        TextView maibo_tv;


    }
}
